<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <div class="query-card">
        <!-- <el-row type="flex" align="middle">
        <el-col :span="12">-->
        <div class="grid-content bg-purple search">
          <el-radio-group v-model="picktime" @change="changeDate">
            <el-radio-button label="1">今天</el-radio-button>
            <el-radio-button label="2">近半月</el-radio-button>
            <el-radio-button label="3">最近一个月</el-radio-button>
          </el-radio-group>
          <el-form ref="query" :model="query" label-width="100px">
            <el-form-item style="margin-bottom:0px" label="关键字">
              <i slot="label" class="el-icon-search" style="font-size:20px"></i>
              <el-input style="width:360px" v-model="query.keywords" placeholder="请输入用户手机号/订单编号搜索"></el-input>
            </el-form-item>
            <el-form-item style="margin-bottom:0px" label="操作日期">
              <el-date-picker
                ref="picker"
                v-model="query.timeArr"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
              ></el-date-picker>
            </el-form-item>
          </el-form>
        </div>
        <!-- </el-col> -->
        <!-- <el-col :span="12"> -->
        <div class="grid-content bg-purple-light">
          <el-button type="primary" @click="queryAsk">查询</el-button>
          <el-button @click="queryReset">重置</el-button>
        </div>
        <!-- </el-col>
        </el-row>-->
      </div>
    </el-card>

    <!-- 表格 -->
    <el-card class="box-card">
      <el-table
        :data="tableData"
        v-loading="loading"
        :header-cell-style="{background:'#eee'}"
        border
        style="width: 100%;margin-bottom: 20px;"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="nickname" label="用户昵称"></el-table-column>
        <el-table-column align="center" prop="mobile" label="手机"></el-table-column>
        <el-table-column align="center" prop="order_sn" label="订单编号"></el-table-column>
        <el-table-column align="center" label="订单来源">
          <template slot-scope="scope">
            <div v-if="scope.row.sorce==1">小程序</div>
            <div v-if="scope.row.sorce==2">APP</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="支付方式">
          <template slot-scope="scope">
            <div v-if="scope.row.pay_type==1">微信</div>
            <div v-if="scope.row.pay_type==2">支付宝</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="pay_sum_price" label="充值金额"></el-table-column>
        <el-table-column align="center" prop="pay_price" label="实付"></el-table-column>
        <el-table-column align="center" prop="pay_time" label="下单时间"></el-table-column>
        <el-table-column align="center" prop="add_time" label="支付时间"></el-table-column>
      </el-table>
    </el-card>
    <div style="text-align:right">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="currentPage"
        :total="total"
        @current-change="currentChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { rechargeOrderList } from "@/api/order";
import { parseTime } from "@/utils/index";

export default {
  name: "RechargeOrder",
  data() {
    return {
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //query
      query: {
        keywords: "",
        user_id: "0",
        add_time: "",
        timeArr: []
      },
      options: "",
      tableData: [],
      loading: false,
      picktime: "1",
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", [new Date(), new Date()]);
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      }
    };
  },
  mounted() {
    this.query.timeArr = [new Date(), new Date()];
    this.getList();
  },
  activated() {
    this.resetForm("query");
    this.total = 0;
    this.currentPage = 1;
    this.pageSize = 10;
    this.query.add_time = "";
    this.query.class_id = "";
    this.query.timeArr = [new Date(), new Date()];
    this.getList();
  },
  methods: {
    async getList() {
      this.loading = true;
      this.query.add_time =
        parseTime(this.query.timeArr[0], "{y}-{m}-{d}") +
        "," +
        parseTime(this.query.timeArr[1], "{y}-{m}-{d}");
      const res = await rechargeOrderList({
        page: this.currentPage,
        limit: this.pageSize,
        keywords: this.query.keywords,
        user_id: this.query.user_id,
        add_time: this.query.add_time
      });
      console.log(res);
      this.loading = false;
      this.total = res.count;
      this.tableData = res.data;
      this.tableData.forEach(item => {
        if (item.add_time == 0) {
          item.add_time = "-";
        } else {
          item.add_time = parseTime(item.add_time, "{y}-{m}-{d} {h}:{i}:{s}");
        }
        if (item.pay_time == 0) {
          item.pay_time = "-";
        } else {
          item.pay_time = parseTime(item.pay_time, "{y}-{m}-{d} {h}:{i}:{s}");
        }
      });
    },
    changeDate() {
      console.log(this.picktime);
      if (this.picktime == 1) {
        this.$set(this.query, "timeArr", [new Date(), new Date()]);
        this.getList();
      } else if (this.picktime == 2) {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
        this.$set(this.query, "timeArr", [start, end]);
        this.getList();
      } else if (this.picktime == 3) {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
        this.$set(this.query, "timeArr", [start, end]);
        this.getList();
      }
    },
    queryAsk() {
      console.log(this.query);
      this.currentPage = 1;
      this.getList();
    },
    queryReset() {
      this.resetForm("query");
      this.total = 0;
      this.currentPage = 1;
      this.pageSize = 10;
      this.query.add_time = "";
      this.query.class_id = "";
      this.query.timeArr = [new Date(), new Date()];
      this.getList();
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current;
      this.getList();
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="scss" scoped>
.query-card {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input--medium .el-input__inner {
  width: 360px;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
::v-deep .el-form {
  display: flex;
}
</style>